//
//  ShopEditViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/10/4.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "ShopEditViewController.h"
#import "EditShopNameViewController.h"
#import "EditOwnNameViewController.h"
#import "ShopLicenseViewController.h"

@interface ShopEditViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ShopEditViewController

- (void)setArray
{
    //@"我的二维码",@"店铺图片"
    self.dataArray = [NSMutableArray array];
    NSArray *array = @[@"店铺头像",@"店铺名称",@"店主姓名",@"店铺地址",@"联系电话",@"店铺实景",@"店主二维码",@"营业执照"];
    [self.dataArray addObjectsFromArray:array];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNaviWith:@"店铺基本资料"];
    self.view.backgroundColor = TotalWhite;
    [self setArray];
    //[self setUpTableView];
}

- (void)getData
{
    self.myData = [NSDictionary dictionary];
    [[HttpRequest currentHttpRequest]postPath:GetShopInfo params:nil resultBlock:^(id Data, MsgModel *msgModel) {
        self.myData = [NSDictionary dictionaryWithDictionary:Data];
        [self.tableView reloadData];
        [self setUpTableView];
    }];
}

- (void)setUpTableView
{
    UIView *lineView = [UIView new];
    if (@available(iOS 11.0, *)) {
        lineView.frame = CGRectMake(0, TopBarHeight_xs, SCREENWIDTH, 10);
    } else {
        // Fallback on earlier versions
        lineView.frame = CGRectMake(0, TopBarHeight, SCREENWIDTH, 10);
    }
    lineView.backgroundColor = HexWithAlpha(0xF0F0F0, 0.53);
    [self.view addSubview:lineView];
    
    if (self.tableView == nil) {
        if (@available(iOS 11.0, *)) {
            self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, lineView.bottom, SCREENWIDTH, SCREENHEIGHT-lineView.bottom-BottomHeight_xs) style:UITableViewStylePlain];
        } else {
            // Fallback on earlier versions
            self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, lineView.bottom, SCREENWIDTH, SCREENHEIGHT-lineView.bottom-BottomHeight) style:UITableViewStylePlain];
        }
        self.tableView.backgroundColor = UIColorFromHex(0xF7F7F7);
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.rowHeight = 57;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        [self.tableView registerClass:[ShopEditTableViewCell class] forCellReuseIdentifier:@"ShopEditTableViewCell"];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];;
        [TTKUtil setAutomaticallyAdjustsScrollViewInsetsWidth:self.tableView withView:self];
        [self.view addSubview:self.tableView];
    }
}
/*
- (UIView *)setFootView
{
    UIView *footView = [UIView new];
    footView.backgroundColor = ClearColor;
    footView.frame = CGRectMake(0, 0, SCREENWIDTH, 110);
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(40, 70, SCREENWIDTH-80, 40);
    sureBtn.backgroundColor = goldColor;
    [sureBtn setTitleColor:titleColor forState:UIControlStateNormal];
    sureBtn.titleLabel.font = FontWithSize(18);
    [sureBtn setAttributedTitle:[TTKUtil getAtttributeStringWithLineSpace:0 kern:2 and:@"保存"] forState:UIControlStateNormal];
    sureBtn.layer.cornerRadius = 15;
    sureBtn.layer.masksToBounds = YES;
    [sureBtn addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:sureBtn];
    return footView;
}

#pragma  mark -- 保存
- (void)sure:(UIButton *)btn
{
    NSLog(@"sure");
}
*/
#pragma mark -- tableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopEditTableViewCell *editCell = [tableView dequeueReusableCellWithIdentifier:@"ShopEditTableViewCell"];
    editCell.indexPath = indexPath;
    editCell.selectionStyle = UITableViewCellSelectionStyleNone;
    editCell.leftLab.text = self.dataArray[indexPath.row];
    if (indexPath.row == 0) {
        [editCell.iconIV sd_setImageWithURL:[NSURL URLWithString:self.myData[@"logo"]] placeholderImage:[UIImage imageNamed:@"Placeholder100"]];
    }
    else if (indexPath.row == 1) {
        editCell.rightLab.text = [NSString stringWithFormat:@"%@",self.myData[@"name"]];
        editCell.arrowIV.hidden = YES;
    }
    else if (indexPath.row == 2) {
        editCell.rightLab.text = [NSString stringWithFormat:@"%@",self.myData[@"ownerName"]];
    }
    else if (indexPath.row == 3) {
        editCell.rightLab.text = [NSString stringWithFormat:@"%@",self.myData[@"address"]];
    }
    else if (indexPath.row == 4) {
        editCell.rightLab.text = [NSString stringWithFormat:@"%@",self.myData[@"ownerMobile"]];
    }
    else if (indexPath.row == 5) {
        editCell.iconIV.image = [UIImage imageNamed:@"qrCode"];
    }
    else if (indexPath.row == 6) {
        editCell.iconIV.image = [UIImage imageNamed:@"qrCode"];
    }
    else if (indexPath.row == 7)
    {
        editCell.iconIV.image = [UIImage imageNamed:@"qrCode"];
        editCell.lineView.hidden = YES;
    }
    return editCell;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 57;
//}

#pragma mark -- 上传单个图片
- (void)uploadImage
{
    [self takePhoto];
    self.photoBlock = ^(UIImage * _Nonnull image) {
        UIImage *imageNew = [TTKUtil imageWithImageSimple:image scaledToSize:CGSizeMake(200, 200)];
        NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.1);
        NSMutableDictionary *dict = [NSMutableDictionary new];
        [dict setValue:imageData forKey:@"file"];
        [[HttpRequest currentHttpRequest]uploadWithParams:dict andImageData:imageData block:^(NSString *SamllPhoto) {
            NSMutableDictionary *data = [NSMutableDictionary dictionary];
            [data setValue:SamllPhoto forKey:@"logo"];
            
            __strong typeof(self) weakSelf = self;
            __strong __typeof(weakSelf)strongSelf = weakSelf;
            [HttpRequest formPostPath:EditMyInfo_API params:data resultBlock:^(id Data, MsgModel *msgModel) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [strongSelf ShowHUDWithMessage:@"修改头像成功"];
                    [strongSelf getData];
                });
            }];
        }];
    };
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            [self uploadImage];
            break;
        case 1:
        {
//            EditShopNameViewController *shopNameVC = [EditShopNameViewController new];
//            shopNameVC.id = [self.myData[@"sellerId"]integerValue];
//            shopNameVC.shopName = self.myData[@"name"];
//            [self.navigationController pushViewController:shopNameVC animated:YES];
            break;
        }
        case 2:
        {
            EditOwnNameViewController *ownVC = [EditOwnNameViewController new];
            ownVC.id = [self.myData[@"sellerId"]integerValue];
            ownVC.ownName = self.myData[@"ownerName"];
            [self.navigationController pushViewController:ownVC animated:YES];
            break;
        }
        case 3:
        {
            EditAddressViewController *editAddress = [EditAddressViewController new];
            editAddress.address = [NSString stringWithFormat:@"%@%@%@%@",self.myData[@"province"],self.myData[@"city"],self.myData[@"district"],self.myData[@"address"]];
            editAddress.id = [self.myData[@"sellerId"]integerValue];
            [self.navigationController pushViewController:editAddress animated:YES];
            break;
        }
        case 4:
        {
            EditMobileViewController *editVc = [EditMobileViewController new];
            editVc.id = [self.myData[@"sellerId"]integerValue];
            editVc.mobile = self.myData[@"ownerMobile"];
            [self.navigationController pushViewController:editVc animated:YES];
            break;
        }
        case 5:
        {
            EditShopImageController *editImageVC = [EditShopImageController new];
            editImageVC.id = [self.myData[@"sellerId"]integerValue];
            editImageVC.realUrl = self.myData[@"shopImage"];
            [self.navigationController pushViewController:editImageVC animated:YES];
            break;
        }
        case 6:
        {
            QRCodeViewController *qrVC = [QRCodeViewController new];
            qrVC.id = [self.myData[@"sellerId"]integerValue];
            qrVC.qrUrl = self.myData[@"sellerWechatQrImage"];
            [self.navigationController pushViewController:qrVC animated:YES];
            break;
        }
        case 7:
        {
            ShopLicenseViewController *licenseVC = [ShopLicenseViewController new];
            licenseVC.id = [self.myData[@"isellerIdd"]integerValue];
            licenseVC.licenceUrl = self.myData[@"licenseImage"];
            [self.navigationController pushViewController:licenseVC animated:YES];
            break;
        }
        default:
            break;
    }
}

@end
